using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using UWBGL_WinForms_Lib1;
using UWBGL_XNA_Lib13;

namespace XNA_SceneNode
{
  class DrawHandlerBig : UWB_XNAWindowHandler
  {
    private UWBGL_UIWindow mWindow;
		private DrawHandlerMinimap _minimap;
		private float mouseWorldX, mouseWorldY;


		public DrawHandlerBig()
    {
      mWindow = new UWBGL_UIWindow(true);

    }

    public bool Initialize(Control PlaceHolder)
    {
      if (!mWindow.ReplaceFormControl(PlaceHolder.Handle))
        return false;

      if (!mWindow.AttachHandler(this))
        return false;

      return true;
    }

    public override void DrawGraphics()
    {
			m_WCWindow = _minimap.getRedboxBounds();
			
      BeginScene();

      loadW2NDCXform();

      
      Program.getModel().drawModel();

      EndSceneAndShow();
    }

		public override void OnMouseButton(bool down, MouseEventArgs e)
		{
			int deviceX = 0, deviceY = 0;
			hardwareToDevice(e.X, e.Y, ref deviceX, ref deviceY);

			switch (e.Button)
			{
				case MouseButtons.Left:
					if (down)
					{
						computeWorldCoordinates(deviceX, deviceY); // world coordinate
						Program.getModel().setTranslation(mouseWorldX, mouseWorldY);
					}
					else
					{

					}
					break;
				case MouseButtons.Right:
					break;
				case MouseButtons.Middle:
					break;
			}
		}

		public override void OnMouseMove(MouseEventArgs e)
		{
			int deviceX = 0, deviceY = 0;
			hardwareToDevice(e.X, e.Y, ref deviceX, ref deviceY);

			switch (e.Button)
			{
				case MouseButtons.Left:
					computeWorldCoordinates(deviceX, deviceY); // world coordinate
					Program.getModel().setTranslation(mouseWorldX, mouseWorldY);
					break;
				case MouseButtons.Right:
					break;
				case MouseButtons.Middle:
					break;
			}
		}



		public void computeWorldCoordinates(int deviceX, int deviceY)
		{
			float zoomBoxHeight = _minimap.getRedboxBounds().height();
			float zoomBoxWidth = _minimap.getRedboxBounds().width();

			float windowWidth = (float)mWindow.Width;
			float windowHeight = (float)mWindow.Height;

			mouseWorldX = (((float)deviceX) - (windowWidth / 2)) * (zoomBoxWidth / windowWidth) + _minimap.getRedboxBounds().getCenter().X;
			mouseWorldY = (((float)deviceY) - (windowHeight / 2)) * (zoomBoxHeight / windowHeight) + _minimap.getRedboxBounds().getCenter().Y;
		}


		public void addReferenceofMinimap(ref DrawHandlerMinimap theMinimap)
		{
			_minimap = theMinimap;
		}
  }
}
